/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

// Warning: Do not use the `let` declarator in this file, it breaks our minification

'use strict';
function onError(error, enableDeveloperTools) {

	console.error('[uncaught exception]: ' + error);

	if (error.stack) {
		console.error(error.stack);
	}
}

function assign(destination, source) {
	return Object.keys(source)
		.reduce(function (r, key) { r[key] = source[key]; return r; }, destination);
}

function parseURLQueryArgs() {
	const search = window.location.search || '';

	return search.split(/[?&]/)
		.filter(function (param) { return !!param; })
		.map(function (param) { return param.split('='); })
		.filter(function (param) { return param.length === 2; })
		.reduce(function (r, param) { r[param[0]] = decodeURIComponent(param[1]); return r; }, {});
}

function main() {
	// const webFrame = require('electron').webFrame;
	const args = parseURLQueryArgs();
	const configuration = JSON.parse(args['config'] || '{}') || {};

	// Correctly inherit the parent's environment
	//assign(process.env, configuration.userEnv);
	window.editingFolder = configuration.folderPath;
	// Get the nls configuration into the process.env as early as possible.
	var nlsConfig = { availableLanguages: { '*': 'zh-cn' }, locale: 'zh-cn' };
	// const config = process.env['VSCODE_NLS_CONFIG'];

	if (nlsConfig._resolvedLanguagePackCoreLocation) {
		let bundles = Object.create(null);
		nlsConfig.loadBundle = function (bundle, language, cb) {
		};
	}

	var locale = 'zh-Hans';
	window.document.documentElement.setAttribute('lang', "zh-Hans");

	window.nodeRequire = require.__$__nodeRequire;

	//define('fs', ['original-fs'], function (originalFS) { return originalFS; }); // replace the patched electron fs with the original node fs for all AMD code

	window.MonacoEnvironment = {};

	const onNodeCachedData = window.MonacoEnvironment.onNodeCachedData = [];
	require.config({
		baseUrl: "/",
		'vs/nls': nlsConfig,
		recordStats: false,
		paths: {
			"vscode-css-languageservice": "vscode-css-languageservice/cssLanguageService",
			"vscode-languageserver-types": "vscode-languageserver-types/main",
			"vscode-languageclient": "vscode-languageclient/main",
			"vscode-languageserver-protocol": "vscode-languageserver-protocol/main",
			"vscode-jsonrpc": "vscode-jsonrpc/main",
			"typescript": "typescript/typescript",
			"vscode-html-languageservice": "vscode-html-languageservice/htmlLanguageService",
			"vscode-uri": "vscode-uri/main"
		},
		//nodeCachedDataDir: configuration.nodeCachedDataDir,
		onNodeCachedData: function () { onNodeCachedData.push(arguments); },
		nodeModules: []
	});

	if (nlsConfig.pseudo) {
		require(['vs/nls'], function (nlsPlugin) {
			nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
		});
	}

	// Perf Counters
	// window.MonacoEnvironment.timers = {
	// 	isInitialStartup: !!configuration.isInitialStartup,
	// 	hasAccessibilitySupport: !!configuration.accessibilitySupport,
	// 	start: configuration.perfStartTime,
	// 	windowLoad: configuration.perfWindowLoadTime
	// };

	require([
		'vs/workbench/workbench.main',
		'vs/nls!vs/workbench/workbench.main',
		'vs/css!vs/workbench/workbench.main'
	], function () {
		require('vs/workbench/electron-browser/main')
			.startup(configuration)
			.done(function () {
				//unbind(); // since the workbench is running, unbind our developer related listeners and let the workbench handle them
			}, function (error) {
				onError(error, false);
			});
	});

}

main();
